package com.ldc.api.location;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ldc.common.utils.DateTimeUtils;
import com.ldc.common.utils.RequestUtils;
import com.ldc.common.utils.StringUtils;
import com.ldc.model.Location;
import com.ldc.model.base.ApiBean;
import com.ldc.model.params.PageParams;
import com.ldc.service.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/8/5
 */
@RestController
@RequestMapping(value = "/location")
public class LocationController {
    @Autowired
    LocationService locationService;


    @PostMapping(value = "/save")
    public ApiBean<?> query(HttpServletRequest request, @RequestBody Location location) {
        String userName = RequestUtils.getUserName(request);
        String userId = RequestUtils.getUserId(request);
        try {
            if (location == null) {
                return ApiBean.failed("非法数据");
            }
            if (location.getLat() == 0 || location.getLng() == 0) {
                return ApiBean.failed("非法数据");
            }
            location.setUserId(userId);
            location.setUserName(userName);
            location.setCreateTime(DateTimeUtils.getTime());
            location.setChangeTime(DateTimeUtils.getTime());
            locationService.save(location);
            return ApiBean.success();

        } catch (Exception error) {
            return ApiBean.failed("添加失败");
        }
    }


    @PostMapping(value = "/list")
    public ApiBean<?> list(@RequestBody PageParams params) {
        try {
            if (params == null) {
                return ApiBean.failed();
            }
            IPage<Location> pageRecord = locationService.findPageByQuery(params.getPage(), params.getOffset(), params.getQuery());
            return ApiBean.successPage(pageRecord);
        } catch (Exception error) {
            return ApiBean.failed("查询失败");
        }
    }
}
